<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>代码差异对比工具</title>
    <link rel="stylesheet" href="../coding/common.css">
    <link href="https://cdn.bootcdn.net/ajax/libs/font-awesome/6.7.2/css/all.min.css" rel="stylesheet">
    <style>
        /* 基础样式和变量 */
        :root {
            --primary-color: #4a6fa5;
            --secondary-color: #6c8ebd;
            --background-color: #f5f7fa;
            --card-background: #ffffff;
            --text-color: #333333;
            --border-color: #e0e0e0;
            --error-color: #e74c3c;
            --success-color: #2ecc71;
            --shadow-color: rgba(0, 0, 0, 0.1);
            --font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            --tip-background: #e3f2fd;
            --diff-added: #d4edda;
            --diff-removed: #f8d7da;
            --diff-added-dark: #1e3a2e;
            --diff-removed-dark: #3a1e1e;
            --arrow-color: #007bff;
            --arrow-hover: #0056b3;
        }

        /* 暗黑模式变量 */
        [data-theme="dark"] {
            --primary-color: #5b8ad6;
            --secondary-color: #7fa3d7;
            --background-color: #1a1a2e;
            --card-background: #222438;
            --text-color: #e0e0e0;
            --border-color: #444444;
            --shadow-color: rgba(0, 0, 0, 0.3);
            --tip-background: #1a2a3a;
            --diff-added: rgba(102, 255, 102, 0.4);
            --diff-removed: rgba(255, 102, 102, 0.4);
        }

        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: var(--font-family);
            background-color: var(--background-color);
            color: var(--text-color);
            line-height: 1.6;
            transition: background-color 0.3s, color 0.3s;
        }

        .container {
            max-width: 1400px;
            margin: 0 auto;
            padding: 20px;
            min-height: 100vh;
            display: flex;
            flex-direction: column;
        }

        /* 头部样式 */
        header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 30px;
            padding-bottom: 15px;
            border-bottom: 1px solid var(--border-color);
        }

        header h1 {
            font-size: 2rem;
            color: var(--primary-color);
        }

        /* 头部控件区域 */
        .header-controls {
            display: flex;
            align-items: center;
            gap: 15px;
        }

        /* 分享按钮 */
        .share-button {
            display: flex;
            align-items: center;
            gap: 8px;
            padding: 2px 6px;
            background-color: var(--secondary-color);
            color: white;
            border-radius: 15px;
            cursor: pointer;
            font-size: 14px;
            transition: background-color 0.3s, transform 0.1s;
        }

        .share-button:hover {
            background-color: var(--primary-color);
        }

        .share-button:active {
            transform: scale(0.98);
        }

        .share-button i {
            font-size: 16px;
        }

        /* 主题切换开关 */
        .theme-toggle {
            position: relative;
        }

        .theme-switch {
            opacity: 0;
            position: absolute;
        }

        .theme-switch-label {
            display: flex;
            align-items: center;
            justify-content: space-between;
            cursor: pointer;
            width: 50px;
            height: 26px;
            background-color: #83d8ff;
            border-radius: 100px;
            position: relative;
            transition: background-color 0.3s;
            padding: 0 5px;
        }

        .theme-switch:checked + .theme-switch-label {
            background-color: #4a4e69;
        }

        .theme-switch-label .fa-sun {
            color: #f39c12;
        }

        .theme-switch-label .fa-moon {
            color: #f1c40f;
        }

        .theme-switch-label .slider {
            position: absolute;
            width: 22px;
            height: 22px;
            border-radius: 50%;
            transition: 0.3s;
            background-color: white;
            box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
            left: 2px;
        }

        .theme-switch:checked + .theme-switch-label .slider {
            transform: translateX(24px);
        }

        /* 隐私提示框样式 */
        .privacy-notice {
            margin-bottom: 20px;
            animation: fadeIn 0.5s ease-in-out;
        }

        .privacy-notice-content {
            display: flex;
            align-items: center;
            background-color: var(--tip-background);
            border-left: 4px solid #2196f3;
            padding: 15px;
            border-radius: 4px;
            box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
            position: relative;
        }

        .privacy-notice-content i.fa-shield-alt {
            font-size: 20px;
            color: #2196f3;
            margin-right: 15px;
        }

        .privacy-notice-content p {
            flex: 1;
            margin: 0;
            font-size: 14px;
            color: var(--text-color);
        }

        .close-notice {
            background: none;
            border: none;
            color: #90a4ae;
            cursor: pointer;
            font-size: 16px;
            padding: 5px;
            display: flex;
            align-items: center;
            justify-content: center;
            transition: color 0.2s;
        }

        .close-notice:hover {
            color: #546e7a;
            background: none;
        }

        @keyframes fadeIn {
            from {
                opacity: 0;
                transform: translateY(-10px);
            }
            to {
                opacity: 1;
                transform: translateY(0);
            }
        }

        /* 主内容区域 */
        main {
            flex: 1;
            display: flex;
            flex-direction: column;
            gap: 20px;
        }

        .diff-container {
            display: flex;
            gap: 20px;
            height: 600px;
        }

        .diff-panel {
            flex: 1;
            background-color: var(--card-background);
            border-radius: 8px;
            box-shadow: 0 4px 6px var(--shadow-color);
            padding: 20px;
            display: flex;
            flex-direction: column;
            position: relative;
        }

        .diff-panel h2 {
            margin-bottom: 15px;
            color: var(--primary-color);
            display: flex;
            align-items: center;
            gap: 10px;
            font-size: 1.2rem;
        }

        .diff-textarea {
            flex: 1;
            height: 100%;
            padding: 15px;
            border: 1px solid var(--border-color);
            border-radius: 4px;
            font-family: 'Consolas', 'Monaco', 'Courier New', monospace;
            font-size: 14px;
            background-color: transparent;
            color: var(--text-color);
            transition: border-color 0.3s, background-color 0.3s, color 0.3s;
            resize: none;
            line-height: 1.5;
            white-space: pre;
            overflow-wrap: normal;
            overflow-x: auto;
            position: relative;
            z-index: 2;
        }

        .diff-textarea:focus {
            outline: none;
            border-color: var(--primary-color);
        }

        /* 差异高亮样式 */
        .diff-textarea.highlighted {
            background: linear-gradient(to right, transparent 0%, transparent 100%);
            background-attachment: local;
        }

        .diff-textarea-wrapper {
            position: relative;
            flex: 1;
            display: flex;
        }

        .line-numbers {
            width: 50px;
            background-color: var(--tip-background);
            border: 1px solid var(--border-color);
            border-right: none;
            border-radius: 4px 0 0 4px;
            padding: 15px 8px;
            font-family: 'Consolas', 'Monaco', 'Courier New', monospace;
            font-size: 14px;
            line-height: 1.5;
            color: var(--text-color);
            opacity: 0.7;
            text-align: right;
            user-select: none;
            white-space: pre;
            overflow: hidden;
        }

        .diff-textarea {
            border-radius: 0 4px 4px 0;
            border-left: none;
        }

        .diff-highlight-overlay {
            position: absolute;
            top: 15px;
            left: 65px;
            right: 15px;
            bottom: 15px;
            pointer-events: none;
            font-family: 'Consolas', 'Monaco', 'Courier New', monospace;
            font-size: 14px;
            line-height: 1.5;
            white-space: pre;
            overflow: hidden;
            border-radius: 4px;
        }

        .diff-highlight-line {
            display: block;
            min-height: 21px;
            margin: 0;
            padding: 0;
        }

        .diff-highlight-line.added {
            background-color: var(--diff-added);
        }

        .diff-highlight-line.removed {
            background-color: var(--diff-removed);
        }

        .diff-highlight-line.modified {
            background-color: var(--diff-added);
        }

        /* 箭头按钮样式 */
        .arrow-container {
            position: absolute;
            z-index: 10;
        }

        .arrow-container.left {
            right: -30px;
        }

        .arrow-container.right {
            left: -30px;
        }

        .arrow-btn {
            background-color: var(--arrow-color);
            color: white;
            border: none;
            border-radius: 50%;
            width: 24px;
            height: 24px;
            cursor: pointer;
            display: flex;
            align-items: center;
            justify-content: center;
            font-size: 12px;
            transition: background-color 0.3s, transform 0.1s;
            margin: 2px 0;
        }

        .arrow-btn:hover {
            background-color: var(--arrow-hover);
            transform: scale(1.1);
        }

        .arrow-btn:active {
            transform: scale(0.95);
        }

        /* 控制按钮区域 */
        .controls {
            display: flex;
            justify-content: center;
            gap: 15px;
            margin: 20px 0;
        }

        .control-btn {
            padding: 12px 24px;
            border: none;
            border-radius: 4px;
            background-color: var(--primary-color);
            color: white;
            cursor: pointer;
            font-size: 16px;
            display: inline-flex;
            align-items: center;
            gap: 8px;
            transition: background-color 0.3s, transform 0.1s;
        }

        .control-btn:hover {
            background-color: var(--secondary-color);
        }

        .control-btn:active {
            transform: scale(0.98);
        }

        .control-btn.clear {
            background-color: var(--error-color);
        }

        .control-btn.clear:hover {
            background-color: #c0392b;
        }

        /* 统计信息 */
        .stats {
            background-color: var(--tip-background);
            border-radius: 4px;
            padding: 15px;
            margin-top: 20px;
            font-size: 14px;
            color: var(--text-color);
            display: none;
        }

        .stats.show {
            display: block;
        }

        /* 通知样式 */
        .notification {
            position: fixed;
            bottom: 20px;
            right: 20px;
            background-color: var(--success-color);
            color: white;
            padding: 10px 20px;
            border-radius: 4px;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.2);
            transform: translateY(100px);
            opacity: 0;
            transition: transform 0.3s, opacity 0.3s;
        }

        .notification.show {
            transform: translateY(0);
            opacity: 1;
        }

        .notification.error {
            background-color: var(--error-color);
        }

        /* 响应式设计 */
        @media (max-width: 1024px) {
            .diff-container {
                flex-direction: column;
                height: auto;
            }

            .diff-panel {
                height: 300px;
            }

            .arrow-container {
                display: none;
            }
        }

        @media (max-width: 768px) {
            .privacy-notice-content {
                flex-direction: column;
                text-align: center;
                padding: 15px 10px;
            }
            
            .privacy-notice-content i.fa-shield-alt {
                margin-right: 0;
                margin-bottom: 10px;
            }
            
            .close-notice {
                position: absolute;
                top: 5px;
                right: 5px;
            }

            header {
                flex-direction: column;
                gap: 15px;
                text-align: center;
            }

            .controls {
                flex-direction: column;
                align-items: center;
            }
        }
    </style>
    <script type="text/javascript" charset="UTF-8" src="https://cdn.wwads.cn/js/makemoney.js" async></script>
</head>
<body>
    <header>
        <div class="header-container">
            <div class="header-left">
                <div class="logo">
                    <span><a href="https://geekdaxue.co/" target="_blank"><img src="https://geekdaxue.co/static/images/logo_platypus.png" height="30px"></a></span>
                </div>
                <nav class="main-navigation">
                    <div class="nav-item dropdown">
                        <a href="#" class="nav-link">
                            JSON工具 <i class="fas fa-chevron-down"></i>
                        </a>
                        <div class="dropdown-menu">
                            <a href="../json/index.html">JSON 格式化</a>
                            <a href="../json/parser.html">JSON 格式校验</a>
                            <a href="../json/excel.html">JSON 转 Excel/CSV</a>
                            <a href="../json/jsonxml.html">JSON 与 XML 转换</a>
                            <a href="../json/editor.html">JSON 编辑器</a>
                        </div>
                    </div>
                    <div class="nav-item dropdown">
                        <a href="#" class="nav-link">
                            编码转换 <i class="fas fa-chevron-down"></i>
                        </a>
                        <div class="dropdown-menu">
                            <a href="../coding/base64.html">Base64 编码</a>
                            <a href="../coding/urlencoding.html">URL 编码</a>
                            <a href="../coding/md5.html">MD5 加密</a>
                            <a href="../coding/unicode.html">Unicode 转换</a>
                            <a href="../coding/hex_convert.html">进制转换</a>
                        </div>
                    </div>
                    <div class="nav-item dropdown">
                        <a href="#" class="nav-link">
                            文本工具 <i class="fas fa-chevron-down"></i>
                        </a>
                        <div class="dropdown-menu">
                            <a href="../txt/diff.html">文本对比</a>
                            <a href="../txt/replace.html">文本替换</a>
                            <a href="../txt/remove.html">空白行移除</a>
                            <a href="../txt/cn_trans.html">繁简体转换</a>
                        </div>
                    </div>
                    <div class="nav-item dropdown">
                        <a href="#" class="nav-link">
                            图片工具 <i class="fas fa-chevron-down"></i>
                        </a>
                        <div class="dropdown-menu">
                            <a href="../img/2svg.html">图片转 SVG</a>
                            <a href="../img/qr_code.html">二维码生成</a>
                            <a href="../img/barcode.html">条形码生成</a>
                        </div>
                    </div>
                </nav>
            </div>
            <div class="header-controls">
                <div class="theme-toggle">
                    <input type="checkbox" id="theme-switch" class="theme-switch">
                    <label for="theme-switch" class="theme-switch-label">
                        <i class="fas fa-sun"></i>
                        <i class="fas fa-moon"></i>
                        <span class="slider"></span>
                    </label>
                </div>
                <div class="share-button" id="share-button" title="分享给朋友">
                    <i class="fas fa-share-alt"></i>
                    <span>分享</span>
                </div>
            </div>
        </div>
    </header>
    <div class="container">
        <div id="privacy-notice" class="privacy-notice">
            <div class="privacy-notice-content">
                <i class="fas fa-shield-alt"></i>
                <p>安全提示：您的数据仅在本地浏览器中处理，不会上传到服务器，请放心使用！</p>
                <button id="close-notice" class="close-notice" title="关闭提示">
                    <i class="fas fa-times"></i>
                </button>
            </div>
        </div>
        
        <main>
            <div class="diff-container">
                <div class="diff-panel">
                    <h2><i class="fas fa-file-code"></i> 左侧代码</h2>
                    <div class="diff-textarea-wrapper">
                        <div id="left-line-numbers" class="line-numbers"></div>
                        <textarea id="left-textarea" class="diff-textarea" placeholder="在此输入左侧代码或文本内容..."></textarea>
                        <div id="left-highlight" class="diff-highlight-overlay"></div>
                    </div>
                    <div id="left-arrows" class="arrow-container left"></div>
                </div>
                
                <div class="diff-panel">
                    <h2><i class="fas fa-file-code"></i> 右侧代码</h2>
                    <div class="diff-textarea-wrapper">
                        <div id="right-line-numbers" class="line-numbers"></div>
                        <textarea id="right-textarea" class="diff-textarea" placeholder="在此输入右侧代码或文本内容..."></textarea>
                        <div id="right-highlight" class="diff-highlight-overlay"></div>
                    </div>
                    <div id="right-arrows" class="arrow-container right"></div>
                </div>
            </div>

            <div class="controls">
                <button id="compare-btn" class="control-btn">
                    <i class="fas fa-code-branch"></i> 对比差异
                </button>
                <button id="clear-btn" class="control-btn clear">
                    <i class="fas fa-eraser"></i> 清空内容
                </button>
            </div>

            <div id="stats" class="stats">
                <i class="fas fa-info-circle"></i> 差异统计信息将在对比后显示
            </div>
        </main>
        <div class="wwads-cn wwads-horizontal" data-id="321" style="max-width:350px"></div>
    </div>
    <footer>
            <div class="footer-container">
                <div class="footer-main">
                    <div class="footer-section about-us">
                        <h3>关于我们</h3>
                        <p>我们提供一系列高效、便捷的在线工具，帮助开发者、非技术人员提高工作效率，解决日常开发中遇到的各种问题。</p>
                        <p>Build With <i class="fa-solid fa-heart" style="color: red;"></i> For SF.</p>
                    </div>
                    <div class="footer-section links">
                        <h3>工具导航</h3>
                        <div class="links-columns">
                            <ul>
                                <li><a href="../json/index.html">JSON 工具</a></li>
                                <li><a href="../img/index.html">图片处理</a></li>
                                <li><a href="#">开发工具</a></li>
                            </ul>
                            <ul>
                                <li><a href="../coding/index.html">编码转换</a></li>
                                <li><a href="../txt/index.html">文本工具</a></li>
                            </ul>
                        </div>
                    </div>
                    <div class="footer-section contact">
                        <h3>联系我们</h3>
                        <div class="social-icons">
                            <a href="#" title="GitHub"><i class="fab fa-github"></i></a>
                            <a href="#" title="Twitter"><i class="fab fa-twitter"></i></a>
                            <a href="#" title="WeChat"><i class="fab fa-weixin"></i></a>
                            <a href="mailto:example@example.com" title="Email"><i class="fas fa-envelope"></i></a>
                        </div>
                        <p>如有问题或建议，请随时联系我们</p>
                    </div>
                </div>
                <div class="footer-bottom">
                    <div class="footer-legal">
                        <a href="#">使用条款</a>
                        <a href="#">隐私政策</a>
                    </div>
                </div>
            </div>
        </footer>
    
    <div id="notification" class="notification"></div>
    
    <script>
        document.addEventListener('DOMContentLoaded', function() {
            // 获取DOM元素
            const leftTextarea = document.getElementById('left-textarea');
            const rightTextarea = document.getElementById('right-textarea');
            const compareBtn = document.getElementById('compare-btn');
            const clearBtn = document.getElementById('clear-btn');
            const statsDiv = document.getElementById('stats');
            const notification = document.getElementById('notification');
            const themeSwitch = document.getElementById('theme-switch');
            const privacyNotice = document.getElementById('privacy-notice');
            const closeNoticeBtn = document.getElementById('close-notice');
            const shareButton = document.getElementById('share-button');
            const leftArrows = document.getElementById('left-arrows');
            const rightArrows = document.getElementById('right-arrows');
            const leftHighlight = document.getElementById('left-highlight');
            const rightHighlight = document.getElementById('right-highlight');
            const leftLineNumbers = document.getElementById('left-line-numbers');
            const rightLineNumbers = document.getElementById('right-line-numbers');
            
            let leftLines = [];
            let rightLines = [];
            let diffs = [];
            
            // 检查本地存储中的主题设置
            if (localStorage.getItem('theme') === 'dark') {
                document.documentElement.setAttribute('data-theme', 'dark');
                themeSwitch.checked = true;
            }
            
            // 主题切换功能
            themeSwitch.addEventListener('change', function() {
                if (this.checked) {
                    document.documentElement.setAttribute('data-theme', 'dark');
                    localStorage.setItem('theme', 'dark');
                } else {
                    document.documentElement.removeAttribute('data-theme');
                    localStorage.setItem('theme', 'light');
                }
            });
            
            // 隐私提示框关闭功能
            closeNoticeBtn.addEventListener('click', function() {
                privacyNotice.style.display = 'none';
                localStorage.setItem('privacyNoticeClosed', 'true');
            });
            
            // 检查是否需要显示隐私提示
            if (localStorage.getItem('privacyNoticeClosed') === 'true') {
                privacyNotice.style.display = 'none';
            }
            
            // 分享功能
            shareButton.addEventListener('click', function() {
                const url = window.location.href;
                navigator.clipboard.writeText(url).then(() => {
                    showNotification('已复制网址，赶快让更多的小伙伴知道我吧！');
                }).catch(() => {
                    showNotification('复制失败，请手动复制网址', true);
                });
            });
            
            // 显示通知
            function showNotification(message, isError = false) {
                notification.textContent = message;
                notification.className = 'notification show' + (isError ? ' error' : '');
                setTimeout(() => {
                    notification.classList.remove('show');
                }, 3000);
            }
            
            // 简单的差异算法
            function computeDiff(left, right) {
                const leftLines = left.split('\n');
                const rightLines = right.split('\n');
                const maxLines = Math.max(leftLines.length, rightLines.length);
                const diffs = [];
                
                for (let i = 0; i < maxLines; i++) {
                    const leftLine = leftLines[i] || '';
                    const rightLine = rightLines[i] || '';
                    
                    if (leftLine !== rightLine) {
                        diffs.push({
                            lineNumber: i,
                            leftLine: leftLine,
                            rightLine: rightLine,
                            type: leftLine === '' ? 'added' : rightLine === '' ? 'removed' : 'modified'
                        });
                    }
                }
                
                return { leftLines, rightLines, diffs };
            }
            
            // 更新行号显示
            function updateLineNumbers(lines, lineNumbersElement) {
                const lineCount = Math.max(lines.length, 1);
                const numbers = [];
                for (let i = 1; i <= lineCount; i++) {
                    numbers.push(i.toString());
                }
                lineNumbersElement.textContent = numbers.join('\n');
            }
            
            // 创建高亮覆盖层
            function createHighlightOverlay(lines, diffs, isLeft) {
                const overlay = isLeft ? leftHighlight : rightHighlight;
                const lineNumbersElement = isLeft ? leftLineNumbers : rightLineNumbers;
                overlay.innerHTML = '';
                
                // 更新行号
                updateLineNumbers(lines, lineNumbersElement);
                
                lines.forEach((line, index) => {
                    const lineDiv = document.createElement('div');
                    lineDiv.className = 'diff-highlight-line';
                    
                    // 检查是否是差异行
                    const diff = diffs.find(d => d.lineNumber === index);
                    if (diff) {
                        if (isLeft) {
                            if (diff.leftLine === '') {
                                lineDiv.classList.add('removed');
                            } else if (diff.rightLine === '') {
                                lineDiv.classList.add('added');
                            } else {
                                lineDiv.classList.add('modified');
                            }
                        } else {
                            if (diff.rightLine === '') {
                                lineDiv.classList.add('removed');
                            } else if (diff.leftLine === '') {
                                lineDiv.classList.add('added');
                            } else {
                                lineDiv.classList.add('modified');
                            }
                        }
                    }
                    
                    // 添加不可见的文本内容以保持行高
                    lineDiv.textContent = line || ' ';
                    lineDiv.style.color = 'transparent';
                    overlay.appendChild(lineDiv);
                });
            }
            
            // 创建箭头按钮组
            function createArrowButtons() {
                // 清除之前的箭头
                leftArrows.innerHTML = '';
                rightArrows.innerHTML = '';
                
                if (diffs.length === 0) return;
                
                // 按差异区域分组
                const diffGroups = [];
                let currentGroup = null;
                
                diffs.forEach((diff, index) => {
                    if (!currentGroup || diff.lineNumber !== currentGroup.endLine + 1) {
                        // 开始新的差异组
                        currentGroup = {
                            startLine: diff.lineNumber,
                            endLine: diff.lineNumber,
                            diffs: [diff],
                            index: index
                        };
                        diffGroups.push(currentGroup);
                    } else {
                        // 扩展当前差异组
                        currentGroup.endLine = diff.lineNumber;
                        currentGroup.diffs.push(diff);
                    }
                });
                
                // 为每个差异组创建箭头
                diffGroups.forEach(group => {
                    const middleLine = Math.floor((group.startLine + group.endLine) / 2);
                    // 计算箭头位置，考虑textarea的padding和行高
                    const arrowTop = 60 + (middleLine * 21); // 15px是textarea的padding-top，21px是行高
                    
                    // 检查是否有左侧内容可以应用到右侧
                    const hasLeftContent = group.diffs.some(d => d.leftLine !== '' && d.leftLine !== undefined);
                    if (hasLeftContent) {
                        const leftArrow = document.createElement('button');
                        leftArrow.className = 'arrow-btn';
                        leftArrow.innerHTML = '<i class="fas fa-arrow-right"></i>';
                        leftArrow.title = `应用到右侧 (行 ${group.startLine + 1}${group.startLine !== group.endLine ? '-' + (group.endLine + 1) : ''})`;
                        leftArrow.style.position = 'absolute';
                        leftArrow.style.top = `${arrowTop}px`;
                        leftArrow.addEventListener('click', () => applyGroupToRight(group));
                        leftArrows.appendChild(leftArrow);
                    }
                    
                    // 检查是否有右侧内容可以应用到左侧
                    const hasRightContent = group.diffs.some(d => d.rightLine !== '' && d.rightLine !== undefined);
                    if (hasRightContent) {
                        const rightArrow = document.createElement('button');
                        rightArrow.className = 'arrow-btn';
                        rightArrow.innerHTML = '<i class="fas fa-arrow-left"></i>';
                        rightArrow.title = `应用到左侧 (行 ${group.startLine + 1}${group.startLine !== group.endLine ? '-' + (group.endLine + 1) : ''})`;
                        rightArrow.style.position = 'absolute';
                        rightArrow.style.top = `${arrowTop}px`;
                        rightArrow.addEventListener('click', () => applyGroupToLeft(group));
                        rightArrows.appendChild(rightArrow);
                    }
                });
            }
            
            // 高亮显示差异
            function highlightDiffs() {
                const leftText = leftTextarea.value;
                const rightText = rightTextarea.value;
                
                if (!leftText.trim() && !rightText.trim()) {
                    showNotification('请输入要对比的内容', true);
                    return;
                }
                
                const result = computeDiff(leftText, rightText);
                leftLines = result.leftLines;
                rightLines = result.rightLines;
                diffs = result.diffs;
                
                // 创建高亮覆盖层
                createHighlightOverlay(leftLines, diffs, true);
                createHighlightOverlay(rightLines, diffs, false);
                
                // 创建箭头按钮
                createArrowButtons();
                
                // 显示统计信息
                const addedCount = diffs.filter(d => d.type === 'added').length;
                const removedCount = diffs.filter(d => d.type === 'removed').length;
                const modifiedCount = diffs.filter(d => d.type === 'modified').length;
                
                statsDiv.innerHTML = `
                    <i class="fas fa-info-circle"></i> 
                    差异统计：共 ${diffs.length} 处差异，
                    新增 ${addedCount} 行，
                    删除 ${removedCount} 行，
                    修改 ${modifiedCount} 行
                `;
                statsDiv.classList.add('show');
                
                showNotification('差异对比完成！');
            }
            
            // 应用差异组到右侧
            function applyGroupToRight(group) {
                group.diffs.forEach(diff => {
                    if (diff.leftLine !== '') {
                        rightLines[diff.lineNumber] = diff.leftLine;
                    }
                });
                rightTextarea.value = rightLines.join('\n');
                highlightDiffs();
                showNotification('已应用到右侧');
            }
            
            // 应用差异组到左侧
            function applyGroupToLeft(group) {
                group.diffs.forEach(diff => {
                    if (diff.rightLine !== '') {
                        leftLines[diff.lineNumber] = diff.rightLine;
                    }
                });
                leftTextarea.value = leftLines.join('\n');
                highlightDiffs();
                showNotification('已应用到左侧');
            }
            
            // 应用到右侧（单行，保留兼容性）
            function applyToRight(lineNumber) {
                rightLines[lineNumber] = leftLines[lineNumber];
                rightTextarea.value = rightLines.join('\n');
                highlightDiffs();
                showNotification('已应用到右侧');
            }
            
            // 应用到左侧（单行，保留兼容性）
            function applyToLeft(lineNumber) {
                leftLines[lineNumber] = rightLines[lineNumber];
                leftTextarea.value = leftLines.join('\n');
                highlightDiffs();
                showNotification('已应用到左侧');
            }
            
            // 清空内容
            function clearContent() {
                leftTextarea.value = '';
                rightTextarea.value = '';
                leftArrows.innerHTML = '';
                rightArrows.innerHTML = '';
                leftHighlight.innerHTML = '';
                rightHighlight.innerHTML = '';
                leftLineNumbers.textContent = '1';
                rightLineNumbers.textContent = '1';
                statsDiv.classList.remove('show');
                leftLines = [];
                rightLines = [];
                diffs = [];
                showNotification('内容已清空');
            }
            
            // 事件监听器
            compareBtn.addEventListener('click', highlightDiffs);
            clearBtn.addEventListener('click', clearContent);
            
            // 键盘快捷键
            document.addEventListener('keydown', function(e) {
                if (e.ctrlKey || e.metaKey) {
                    switch(e.key) {
                        case 'Enter':
                            e.preventDefault();
                            highlightDiffs();
                            break;
                        case 'Delete':
                        case 'Backspace':
                            if (e.shiftKey) {
                                e.preventDefault();
                                clearContent();
                            }
                            break;
                    }
                }
            });
            
            // 自动对比（当内容变化时）
            let debounceTimer;
            function autoCompare() {
                clearTimeout(debounceTimer);
                debounceTimer = setTimeout(() => {
                    if (leftTextarea.value.trim() || rightTextarea.value.trim()) {
                        highlightDiffs();
                    }
                }, 1000);
            }
            
            // 同步滚动和行号更新
            function syncScroll(source, target, lineNumbers) {
                target.scrollTop = source.scrollTop;
                lineNumbers.scrollTop = source.scrollTop;
            }
            
            function updateLineNumbersOnInput(textarea, lineNumbers) {
                const lines = textarea.value.split('\n');
                updateLineNumbers(lines, lineNumbers);
            }
            
            leftTextarea.addEventListener('scroll', () => {
                syncScroll(leftTextarea, rightTextarea, leftLineNumbers);
            });
            
            rightTextarea.addEventListener('scroll', () => {
                syncScroll(rightTextarea, leftTextarea, rightLineNumbers);
            });
            
            leftTextarea.addEventListener('input', () => {
                updateLineNumbersOnInput(leftTextarea, leftLineNumbers);
                autoCompare();
            });
            
            rightTextarea.addEventListener('input', () => {
                updateLineNumbersOnInput(rightTextarea, rightLineNumbers);
                autoCompare();
            });
            
            // 初始化行号
            updateLineNumbers([''], leftLineNumbers);
            updateLineNumbers([''], rightLineNumbers);
        });
    </script>
</body>
</html>